package com.shopReduce.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.shopReduce.RestExceptionHandler;
import com.shopReduce.bean.ReduceProgramme;
import com.shopReduce.service.IReduceProgrammeService;

import shopBean.common.Enum.StatusAll;
import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.RestResultGenerator;

@RestController
@RequestMapping("/reduceProgrmme/")
public class ReduceProgrammeContoller {
	@Autowired
	IReduceProgrammeService reduceProgrammeService;
	private static final Logger log = LoggerFactory.getLogger(ReduceProgrammeContoller.class);
	@RequestMapping("insert.api")
	public RestResult insert( @RequestBody ReduceProgramme reduceProgramme) {
		
		log.debug(reduceProgramme.toString());
		return reduceProgrammeService.insert(reduceProgramme);
	}
	
	@RequestMapping("listManager.api")
	public RestResult list( @RequestBody ReduceProgramme reduceProgramme) {
		log.debug(reduceProgramme.toString());
		if(reduceProgramme.getStart() == null) {
			reduceProgramme.setStart(new Long(1));
		}
		if(reduceProgramme.getRow() == null) {
			reduceProgramme.setRow(new Long(10));
		}
		reduceProgramme.setStart((reduceProgramme.getStart()-1)*reduceProgramme.getRow());
		RestResult<List<ReduceProgramme>> result = RestResultGenerator.genSuccessResult(reduceProgrammeService.list(reduceProgramme));
		result.setCount(reduceProgrammeService.listCount(reduceProgramme));
		return result;
	}
	
	@RequestMapping("get.api")
	public RestResult getApi (@RequestParam(value="id") String id) {
		return RestResultGenerator.genSuccessResult(this.reduceProgrammeService.get(id));
	}
	
	@RequestMapping("get.do")
	public RestResult get (@RequestParam(value="id") String id) {
		return RestResultGenerator.genSuccessResult(this.reduceProgrammeService.get(id));
	}
	@RequestMapping("/updateStatus.api")
	public RestResult updateStatus( String id,StatusAll status) {
		return (this.reduceProgrammeService.updateStatus(id, status));
	}
	@RequestMapping("/getByShopId.do")
	public RestResult<List<ReduceProgramme>> getByShopId( String shopId) {
		if(StringUtils.isBlank(shopId)) {
			return RestResultGenerator.genError("商品id不能为空");
		}
		return this.reduceProgrammeService.getByShopIdAndStatus(shopId, StatusAll.normal);
	}
	
	@RequestMapping("getFirstByShopIdAndStatus.api")
	public RestResult<ReduceProgramme> getFirstByShopIdAndStatus( String shopId) {
		return (this.reduceProgrammeService.getFirstByShopIdAndStatus(shopId, StatusAll.normal));
	}
}
